//
// Created by root on 10/10/15.
//
#include <ctype.h>
#include <sys/stat.h>
#include <string.h>
#include "common.h"
#include "my_functon.h"

#define IS_SPACE(c)  (c == ' ' || c == '\t')
char* trim(char* str)
{
    char* start=NULL, *end=NULL;
    if(str==NULL) return NULL;
    while( IS_SPACE(*str) ) str++;
    for(start=str;(*str)!='\0';++str)
    {
        if ( !IS_SPACE(*str) )
            end=str;
    }
    if(end)  *(end+1) = '\0';
    return start;
}


char* strtolow(char * s){
    int len = strlen(s),i;
    for(i=0;i < len;i++){
        *(s+i) = tolower(*(s+i));
    }
    return s;
}

char* strtoupper(char * s){
    int len = strlen(s),i;
    for(i=0;i < len;i++){
        *(s+i) = toupper(*(s+i));
    }
    return s;
}

/* this will change s ,if you don't want to,copy it first */
UT_array * strsplit(char * s, const char * delimiter){

    char *q = s;
    int len = strlen(s);
    UT_array * strs = NULL;
    utarray_new(strs,&ut_str_icd);

    while (strsep(&q, delimiter));

    for (q = s; q < (s + len);) {

        utarray_push_back(strs,&q);

        //skip to next string
        for (q += strlen(q); q < (s +len) && !*q; q++);

    }

    return  utarray_len(strs) > 0 ? strs : NULL;
}

char * my_strdup(char *s){
    char * dst = (char*)malloc(strlen(s)+1);
    strcpy(dst,s);
    return dst;
}


int file_size(char* filename)
{
    struct stat statbuf;
    stat(filename,&statbuf);
    return statbuf.st_size;
}
